<?php
// +----------------------------------------------------------------------
// | ZanPianCMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.zanpian.com All rights reserved.
// +----------------------------------------------------------------------
// | BBS:  <http://www.feifeicms.cc>
// +----------------------------------------------------------------------
namespace app\common\model;
use app\common\library\Insert;
use think\Model;
class Collect extends Model{	
	public function vod_insert($vod,$mustup,$status=""){
		//播放组名替换
		$vod['vod_play'] =$this->xml_play_replace($vod['vod_play']);		
		//判断本地播放器是否存在
		if(config('contrast_play')){
	    $playurls=$this->xml_all_play($vod['vod_play'],$vod['vod_url']);
		$vod['vod_play'] = !empty($playurls['vod_play']) ? $playurls['vod_play'] : "";
	    $vod['vod_url'] =  !empty($playurls['vod_url']) ? $playurls['vod_url']: "";	
		}
		if(empty($vod['vod_name'])){
			$return['msg']="影片名称为空,跳过";				
			return $return;
		}
		if(!config('empty_playurl') && empty($vod['vod_url'])){
			$return['msg']="播放地址为空,跳过";				
			return $return;
		}		
		if(!$vod['vod_cid']){
			$return['msg']="未匹配到对应栏目分类,跳过";				
			return $return;
		}
		//过滤为空的播放名与播放地址
		$vod['vod_play']=trim(implode('$$$',array_filter(explode('$$$',$vod['vod_play']))),'$$$');
		$vod['vod_url']=trim(implode('$$$',array_filter(explode('$$$',$vod['vod_url']))),'$$$');
		//数据处理
		$vod['vod_mcid'] = model('Mcat')->get_mcid($vod['vod_cid'],$vod['vod_keywords']);
		//开启采集获取关键词
		if(config('keywords_opencai') && !empty($vod['vod_name'])){
		$vod['vod_skeywords']=seokeywords($vod['vod_name'],config('keywords_opencai'));
		}
		if ($vod['vod_filmtime']){
			if(is_int($vod['vod_filmtime']) && strlen($vod['vod_filmtime'])>=10){
			$vod['vod_filmtime']=$vod['vod_filmtime'];	
			}else{
			$vod['vod_filmtime']=strtotime($vod['vod_filmtime'].' 0:0:0');
			}
		}		
		// 要查询检查的字段
		//$field = 'vod_id,vod_cid,vod_name,vod_title,vod_actor,vod_continu,vod_isend,vod_total,vod_inputer,vod_play,vod_url';		
		// 查询来源相同的数据
		$reurl=array_unique(explode(',',$vod['vod_reurl']));
		$array = db('urls')->alias('u')->join('vod v','v.vod_id=u.urls_id','RIGHT')->where('urls_reurl','in',$reurl)->find();
		$vod['vod_reurl']=implode(',',$reurl);
		if($array){
			return $this->vod_update($vod, $array, $mustup);
		}
		$array = db('vod')->where(["vod_cid"=>$vod['vod_cid'],'vod_name'=>$vod['vod_name']])->find();
		if($array){
			//无主演 或 演员完全相等时 更新该影片
			if(empty($vod['vod_actor']) || ($array['vod_actor'] == $vod['vod_actor'])){
				return $this->vod_update($vod, $array, $mustup);
			}
			//有相同演员时更新该影片
			$arr_actor_1 = explode(',', format_vodactor($vod['vod_actor']));
			$arr_actor_2 = explode(',', format_vodactor($array['vod_actor']));
			if(array_intersect($arr_actor_1,$arr_actor_2)){
				return $this->vod_update($vod,$array,$mustup);
			}
		}
        //相似条件判断
		if(config('player_collect_name')){
			$length = ceil(strlen($vod['vod_name'])/3) - intval(config('player_collect_name'));
			if($length > 1){
				$where = array();
				$where['vod_cid'] = array('eq',$vod['vod_cid']);
				$where['vod_name'] = array('like',msubstr($vod['vod_name'],0,$length).'%');
				$array = db('vod')->where($where)->order('vod_id desc')->find();
				if($array){
					// 主演完全相同 则检查是否需要更新
					if(!empty($array['vod_actor']) && !empty($vod['vod_actor']) ){
						$arr_actor_1 = explode(',', format_vodactor($vod['vod_actor']));
						$arr_actor_2 = explode(',', format_vodactor($array['vod_actor']));
						if(!array_diff($arr_actor_1,$arr_actor_2) && !array_diff($arr_actor_2,$arr_actor_1)){
							//若差集为空演员完全相同更新
							return $this->vod_update($vod,$array,$mustup);
						}						
						$oldcount=count($arr_actor_1);
						$newcount=count($arr_actor_2);
						$min=min($oldcount,$newcount);
						if(array_intersect($arr_actor_1,$arr_actor_2) && count(array_intersect($arr_actor_1,$arr_actor_2)) >= $min){
							//判断演员交集并且大于其中最小的统计
							return $this->vod_update($vod,$array,$mustup);
						}
					}
					// 不是同一资源库 则标识为相似待审核
					if(!in_array($vod['vod_inputer'],$array)){
						$vod['vod_status'] = -1;
					}
				}
			}
		}
		//是否锁定该资源
		if($status){
            $return['msg']="资源锁定,不添加新数据,跳过";				
			return $return;			
         }
		unset($vod['vod_id']);//销毁采集中的视频ID变量
		if(empty($vod['vod_hits'])){
			$vod['vod_hits'] = mt_rand(0,config('rand_hits'));
		}
		if(empty($vod['vod_up'])){
			$vod['vod_up'] = mt_rand(0,config('rand_updown'));
		}
		if(empty($vod['vod_down'])){
			$vod['vod_down'] = mt_rand(0,config('rand_updown'));
		}
		if( empty($vod['vod_gold']) ){
			$vod['vod_gold'] = mt_rand(0,config('rand_gold'));
		}
		if(empty($vod['vod_golder']) ){
			$vod['vod_golder'] = mt_rand(0,config('rand_golder'));
		}
		if(config('caiji_datatime')){
			$vod['vod_addtime'] =!empty($vod['vod_addtime']) ? strtotime($vod['vod_addtime']) : time();
		}else{
			$vod['vod_addtime']=time();
		}
			
		// 随机伪原创
		//if(config('play_collect')){
		//	$vod['vod_content'] = ff_rand_str($vod['vod_content']);
		//}	
		$vod['vod_stars'] = 1;
		$img = model('Img');
		$vod['vod_pic']=$img->down_load(trim($vod['vod_pic']),'vod');	
		$vod['vod_letter'] = getletter($vod['vod_name']);
		$vod['vod_letters'] = getletters(trim($vod['vod_name']),1);
		$id=db('vod')->insertGetId($vod);
		//关联写入
		if($id){
			//关联写入剧情
			if(!empty($vod['story_content'])){
			   $storyadd= new Insert;
			   $storyadd->story_add($vod,$id);	
			}
			// 自动获取关键词tag
			if(empty($vod['vod_keywords']) && config('auto_tag')){
				$vod['vod_keywords'] = zanpian_tag_auto($vod["vod_name"], $vod["vod_content"]);
			}
			//关联写入TAG
			if($vod["vod_keywords"]){
				model('Tag')->tag_update($id,$vod["vod_keywords"],1);
			}
			//关联写入类型
			if($vod["vod_mcid"]){
				model('Mcid')->mcid_update($id,$vod["vod_mcid"],1);
			}
			//关联写入属性
			if($vod["vod_prty"]){
				model('Prty')->prty_update($id,$vod["vod_prty"],1);
			}
			//关联写入周期
			if($vod["vod_weekday"]){
				model('Weekday')->weekday_update($id,$vod["vod_weekday"],1);
			}	
			//关联写入电台
			if($vod["vod_diantai"]){
				model('Vodtv')->tv_update($id,$vod["vod_diantai"],1);
			}
			//关联入库到演员视图表
			if($vod['vod_actor']){
				model('Actors')->actors_update($id,$vod['vod_actor'],1);
			}
			//管理入库到演员表视图
            if($vod['vod_director']){
				model('Actors')->actors_update($id,$vod['vod_director'],2);
			}
			//管理入库到来源视图方便记录多个来源的查询
			if($vod['vod_reurl']){
				model('Urls')->urls_update($id,$vod['vod_reurl']);
		    }
            $return['code']=$id;
            $baidutui="";			
			if(config('baidu_tui') && config('cai_baidutui')){
	            $vodurl=zanpian_data_url('home/vod/read',array('id'=>$id,'pinyin'=>$vod['vod_letters'],'cid'=>$vod['vod_cid'],'dir'=>getlistname($vod['vod_cid'],'list_dir'),'jumpurl'=>$vod['vod_jumpurl']));
				$baidutui=baidutui($vodurl);
	        }
            $return['msg']="视频添加成功".$baidutui;			
			return $return;
	  }
	  $return['msg']="视频添加失败";				
	  return $return;
	}
	//视频更新
	public function vod_update($vod,$vod_old,$mustup=false){
		//关联写入剧情
		if(!empty($vod['story_content'])){
			$storyadd= new Insert;
		    $storyadd->story_add($vod,$vod_old['vod_id']);	
		}	
		$img = model('Img');
		// 检测是否站长手动锁定更新
		if('zanpian' == $vod_old['vod_inputer']){
			$return['msg']="站长设置不更新,跳过";				
			return $return;
		}	
		$edit = array();
		if(config('caiji_play')){
		   $play_url = $this->update_play_url($vod,$vod_old);	
		    if($play_url == false){
			   $return['msg']="播放地址组未变化,跳过";				
			   return $return;
		    }else{
			   $edit = $play_url;
		    }
		}else{
			$edit['vod_update_info']="<font color='#e74c3c'>不更新与增加播放地址</font>";
		}
		$edit['vod_url']=trim($edit['vod_url'],'$$$');
		$edit['vod_play']=trim($edit['vod_play'],'$$$');
		//获取原数据来源并转为字符串字符串
		// 组合更新条件及内容(以最后一次更新的库为检测依据)
		$edit['vod_id'] = $vod_old['vod_id'];
		$edit['vod_name'] = $vod['vod_name'];
		$edit['vod_inputer'] = $vod['vod_inputer'];
		$edit['vod_reurl'] = implode(',',array_unique(explode(',',$vod['vod_reurl'].','.$vod_old['vod_reurl'])));
		if(config('caiji_addtime')){
		$edit['vod_addtime'] = time();
		}		
		//原数据中不存在新的资料中存在的字段就更新
		if($vod['vod_total'] && empty($vod_old['vod_total'])){
		$edit['vod_total'] = $vod['vod_total'];	
		}
		if($vod['vod_area'] && empty($vod_old['vod_area'])){
		$edit['vod_area'] = $vod['vod_area'];	
		}
		if($vod['vod_year'] && empty($vod_old['vod_year'])){
		$edit['vod_year'] = $vod['vod_year'];	
		}	
		if($vod['vod_aliases'] && empty($vod_old['vod_aliases'])){
		$edit['vod_aliases'] = $vod['vod_aliases'];	
		}	
		if($vod['vod_doubanid'] && empty($vod_old['vod_doubanid'])){
		$edit['vod_doubanid'] = $vod['vod_doubanid'];	
		}
		if($vod['vod_language'] && empty($vod_old['vod_language'])){
		$edit['vod_language'] = $vod['vod_language'];	
		}
		if($vod['vod_isend'] && empty($vod_old['vod_isend'])){
		$edit['vod_isend'] = $vod['vod_isend'];	
		}	
		if($vod['vod_diantai'] && empty($vod_old['vod_diantai'])){
		$edit['vod_diantai'] = $vod['vod_diantai'];	
		model('Vodtv')->tv_update($vod_old['vod_id'],$vod["vod_diantai"],1);
		}
		if($vod['vod_isfilm'] && empty($vod_old['vod_isfilm'])){
		$edit['vod_isfilm'] = $vod['vod_isfilm'];	
		}
		if($vod['vod_weekday'] && empty($vod_old['vod_weekday'])){
		$edit['vod_weekday'] = $vod['vod_weekday'];	
		}	
		if($vod['vod_tvcont'] && empty($vod_old['vod_tvcont'])){
		$edit['vod_tvcont'] = $vod['vod_tvcont'];
		}
		if($vod['vod_director'] && empty($vod_old['vod_director'])){
		$edit['vod_director'] = $vod['vod_director'];
		model('Actors')->actors_update($vod_old['vod_id'],$vod['vod_director'],2);
		}
		if($vod['vod_actor'] && empty($vod_old['vod_actor'])){
		$edit['vod_actor'] = $vod['vod_actor'];	
		model('Actors')->actors_update($vod_old['vod_id'],$vod['vod_actor'],1);
		}
		if($vod['vod_filmtime'] && empty($vod_old['vod_filmtime'])){
		$edit['vod_filmtime'] = $vod['vod_filmtime'];	
		}
		if($vod['vod_mcid'] && empty($vod_old['vod_mcid'])){
		$edit['vod_mcid'] = $vod['vod_mcid'];	
		model('Mcid')->mcid_update($vod_old['vod_id'],$vod["vod_mcid"],1);
		}
		if($vod['vod_content'] && empty($vod_old['vod_content'])){
		$edit['vod_content'] = $vod['vod_content'];	
		}
		//是否强制更新远程图片
		if(config('caiji_pic') && strstr($vod_old['vod_pic'],"http://")){
		$edit['vod_pic']=$img->down_load(trim($vod['vod_pic']),'vod');		
		}
		// 是否为强制更新资料图片等参数
		if($mustup){
			$edit['vod_pic'] = $img->down_load(trim($vod['vod_pic']),'vod');
           if($vod['vod_area']){$edit['vod_area'] = $vod['vod_area'];}
           if($vod['vod_year']){$edit['vod_year'] = $vod['vod_year'];}
		   if($vod['vod_aliases']){$edit['vod_aliases'] = $vod['vod_aliases'];}
		   if($vod['vod_doubanid']){$edit['vod_doubanid'] = $vod['vod_doubanid'];}
           if($vod['vod_language']){$edit['vod_language'] = $vod['vod_language'];}
           if($vod['vod_total']){$edit['vod_total'] = $vod['vod_total'];}
           if($vod['vod_isend']){$edit['vod_isend'] = $vod['vod_isend'];}
           if($vod['vod_diantai']){$edit['vod_diantai'] = $vod['vod_diantai'];}
           if($vod['vod_isfilm']){$edit['vod_isfilm'] = $vod['vod_isfilm'];}
           if($vod['vod_weekday']){$edit['vod_weekday'] = $vod['vod_weekday'];}
           if($vod['vod_tvcont']){$edit['vod_tvcont'] = $vod['vod_tvcont'];}
           if($vod['vod_director']){
			   $edit['vod_director'] = trim($vod['vod_director'], ',');
			   model('Actors')->actors_update($vod_old['vod_id'],$vod['vod_director'],2);
		   }
           if($vod['vod_actor']){
			   $edit['vod_actor'] = trim($vod['vod_actor'], ',');
		       model('Actors')->actors_update($vod_old['vod_id'],$vod['vod_actor'],1);
		   }
           if($vod['vod_filmtime']){$edit['vod_filmtime'] = $vod['vod_filmtime'];}	
		   if($vod['vod_mcid']){
		   $edit['vod_mcid'] = $vod['vod_mcid'];
		   model('Mcid')->mcid_update($vod_old['vod_id'],$vod["vod_mcid"],1);
		   }	
		}		
		//需要更新的字段
		if($vod['vod_title']){
		$edit['vod_title'] = $vod['vod_title'];
		}
		if($vod['vod_continu'] > $vod_old['vod_continu'] || $vod['vod_continu'] != $vod_old['vod_continu']){
		$edit['vod_continu'] = $vod['vod_continu'];	
		}
		//更新来源地址
		model('Urls')->urls_update($vod_old['vod_id'],$edit['vod_reurl']);	
	    $result = db('vod')->update($edit);
		if($result !== false) {
			  $return['code']=$vod_old['vod_id'];
			  $return['msg']=$edit['vod_update_info'];
			  return $return;
		}else{
			  $return['msg']="数据处理失败,跳过";				
			  return $return;
		}		
		
	}
	
	public function update_play_url($vod,$vod_old){
		$edit = array();
		$edit['vod_isall'] = true; //是否需要整个播放器地址组比较
		// 只有一组播放器且地址相同直接跳出
		if($vod_old['vod_url'] == $vod['vod_url']){
			return false;
		}
		// 原数据库无任何播放地址地址添加
		if(!trim($vod_old['vod_play'])){
			$edit['vod_play'] = $vod['vod_play'];
			$edit['vod_url'] = $vod['vod_url'];
			$edit['vod_update_info'] = strtoupper($vod['vod_play']).' 为新添加的第一组播放地址。';
			//echo  strtoupper($vod['vod_play']).' 为新添加的第一组播放地址。';
			return $edit;
		}
		// 分解原服务器组
		$array_play_old = explode('$$$', strtolower($vod_old['vod_play']));
		$array_url_old = explode('$$$', $vod_old['vod_url']);
		$array_play_vod = explode('$$$', strtolower($vod['vod_play']));
		$array_url_vod = explode('$$$', $vod['vod_url']);		
		// 原数据库无播放地址
		if(!$array_url_old[0]){
			$edit['vod_play'] = $vod['vod_play'];
			$edit['vod_url'] = $vod['vod_url'];
			$edit['vod_update_info'] = strtoupper($vod['vod_play']).' 为新添加的第一组播放地址。';	
			//echo strtoupper($vod['vod_play']).' 为新添加的第一组播放地址。';	
			return $edit;
		}
		// 将原数据库的播放器与地址组合为一一对应的二维数组
		$arrayold = array();
		$arrayold=$this->xml_play_group($array_play_old,$array_url_old);
		// 将一一对应的二维数组去除重复相同的地址
		foreach($arrayold as $key=>$value){
			$arrayold[$key] = array_unique($value);
		}
		// 将采集来的播放器与地址组合为一一对应的二维数组
		$arrayvod = array();
		$arrayvod=$this->xml_play_group($array_play_vod,$array_url_vod);
		// 将一一对应的二维数组去除重复相同的地址
		foreach($arrayvod as $key=>$value){
			$arrayvod[$key] = array_unique($value);
		}		
		//对原数据库组合的播放地址进行循环匹配采集的数据库组合
		foreach($arrayold as $key=>$value){
			//匹配采集的播放组是否存在于原数据库 存在进行一一对比不存在不做处理
			if($arrayvod[$key]){
			        $arraynew[$key] = $this->xml_update_urlone($value[0],$arrayvod[$key][0]);
			        if($arrayold[$key][0] == $arraynew[$key]){
				       $msg[$key]=strtoupper($key).' 数据未变化。';
			        }else{
					   $msg[$key]="更新".strtoupper($key).'成功 ';	
					}
			    unset($arrayvod[$key]);//销毁采集来的相同播放组变量		
			}else{
			    $arraynew[$key]=$value[0];
			}
		}
		//循环采集回来的剩余播放组输出状态
		foreach($arrayvod as $key=>$value){
			//echo ' 新添'.strtoupper($key).' ';
			$msg[$key]='新添'.strtoupper($key).' ';
			$arrayvod[$key]=$value[0];
		}
		
		$edit['vod_update_info']=implode(' ',$msg);
		//处理后的数据与采集中剩余的播放数组合并
        $newarray = array_merge($arraynew,$arrayvod); 
		$newplay=$this->xml_play_reform($newarray);
        $edit['vod_play'] = implode('$$$',$newplay['vod_play']);
		$edit['vod_url'] = implode('$$$',$newplay['vod_url']);
		return $edit;
	}				
    // 重生成某一组的播放地址 返回新的地址(string)
	public function xml_update_urlone($vodurlold, $vodurlnew){
		$arrayold = explode(chr(13),trim($vodurlold));
		$arraynew = explode(chr(13),trim($vodurlnew));
		foreach($arraynew as $key=>$value){
			unset($arrayold[$key]);
		}
		if($arrayold){
			return implode(chr(13),array_merge($arraynew,$arrayold));
		}else{
			return implode(chr(13),$arraynew);
		}
	}	
	//播放组名替换
	public function xml_play_replace($play){
		if(config('replace_play')){
		$replace_play=config('replace_play');
        $replace_key=array_keys($replace_play);
		$replace_values=array_values($replace_play);
		$play=str_replace($replace_key,$replace_values,$play);
		}
     return $play;
	}
	//过滤本地不存在的播放组
	function xml_all_play($play,$url){
		$array_allplay=F('_data/statusplay');
        $array_play = explode('$$$', $play);
		$array_url=explode('$$$', $url);
		$array_newplay=array_intersect($array_play,$array_allplay);
		if($array_newplay){
            foreach($array_newplay as $key=>$value){
			   $array_newurl[] = $array_url[$key];
		   }
		    $new['vod_play']=implode('$$$', $array_newplay);
		    $new['vod_url']=implode('$$$', $array_newurl);
		    return $new; 
	    }else{
		    return false;
		}
	}		
	//组合播放名与播放地址为二维数组
	public function xml_play_group($play,$url){
      if($play && $url ){
	   foreach($play as $key=>$value){
			$arrayold[$value][] = $url[$key];
		}
		return array_filter($arrayold);
	  }else{
		 return false;   
		  }
	}	
	//重新组合播放组
	public function xml_play_reform($newarray){
	 if($newarray){
	   foreach($newarray as $key=>$value){
			$new['vod_play'][] = strtolower($key);
			$new['vod_url'][] = $value;
	   }
	   return $new;  
	 }else{
	   return false;  
		 }
	}
	
	
}